package com.jatec.gsat;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public abstract class AbstractActivity extends Activity {

	protected abstract Class getActivityClass(); 
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {	
		if( item.getItemId() == R.id.mnu_exit){
			this.finish();		
		}
		else if( item.getItemId() == R.id.mnu_german){
			changeLanguage("de");
		}
		else if( item.getItemId() == R.id.mnu_english){
			changeLanguage("en");	
		}
		return true;
	}
	

	
	
	private void changeLanguage(String languageToLoad){
	    
	    
	    //String languageToLoad  = "de";  
	    Locale locale = new Locale(languageToLoad);   
	    Locale.setDefault(locale);  
	    Configuration config2 = new Configuration();  
	    config2.locale = locale;  
	    getBaseContext().getResources().updateConfiguration(config2,   getBaseContext().getResources().getDisplayMetrics()); 
	    
	    changeLanguage(locale);
	    //onlanguageChange();
	}
	
	protected abstract void changeLanguage(Locale locale);
	
	private void onlanguageChange(){
		this.finish();
		Intent intent = new Intent(this.getApplicationContext(), getActivityClass());
		intent.setAction(Intent.ACTION_CONFIGURATION_CHANGED);
		startActivity(intent);
		
	}	
}
